package event

import (
	"gitchina/CPTT4WeChat/errors"
	"gitchina/CPTT4WeChat/util"
)

type TextEvent struct {
	*WeChatRequestMessage
	Content   string
	MessageId string
}

func (e *TextEvent) Handle() interface{} {
	rsMsg := errors.Invalid_Options_Input
	if cache, ok := util.GetCache(util.User_Channel); ok {
		if val, ok := cache.Get(e.FromUserName); ok {
			fn := val.(func(input string) string)
			rsMsg = fn(e.Content)
		} else {
			util.GetLogger().Error("[event-text] FromUserName:%s not found.", e.FromUserName)
		}
	} else {
		util.GetLogger().Error("[event-text] cache not found.")
	}
	return CreateTextResponseMessage(e.WeChatRequestMessage, rsMsg)
}

func (e *TextEvent) OnParse(params map[string]string) Event {
	e.Content = params["content"]
	e.MessageId = params["msgd"]
	return e
}
